﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmTypeOfLeaveWorkInfo : Form
    {
        bool isChange = false;
        TypeOfLeaveWorkInfo _TypeOfLeaveWorkInfo;
        DialogResult dlgResult;
        RoleRightInfo listRole;
        TypeOfLeaveWorkClient wcfTypeOfLeaveWork;
        public frmTypeOfLeaveWorkInfo(int TypeOfLeaveWorkID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            LoadLanguage(); 
            dlgResult = DialogResult.Cancel;
            _TypeOfLeaveWorkInfo = new TypeOfLeaveWorkInfo();
            wcfTypeOfLeaveWork = Service.TypeOfLeaveWork();
            _TypeOfLeaveWorkInfo.ID = TypeOfLeaveWorkID;

            if (TypeOfLeaveWorkID > 0)
            {
                _TypeOfLeaveWorkInfo = wcfTypeOfLeaveWork.TypeOfLeaveWork_GetInfo(TypeOfLeaveWorkID);
                Info2Form();
            }
            listRole = _listRole;
            if (!listRole._create)
                btnNew.Enabled = false;
            if (!listRole.Update)
                btnSave.Enabled = false;
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            label1.Text = UpdateUI.GetString(label1.Tag.ToString());
        }

        void Info2Form()
        {
            txtName.Text = _TypeOfLeaveWorkInfo.Name;
            txtCode.Text = _TypeOfLeaveWorkInfo.Code;
            isChange = false;
        }

        void Form2Info()
        {
            _TypeOfLeaveWorkInfo.Name = txtName.Text.Trim();
            _TypeOfLeaveWorkInfo.Code = txtCode.Text.Trim();
            _TypeOfLeaveWorkInfo.BranchCode = Program.strBranchCode;
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            if (txtCode.Text.Trim().Length == 0)
            {
                txtCode.Focus();
                MessageBox.Show("Chưa nhập mã hình thức thôi việc", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (txtName.Text.Trim().Length == 0)
            {
                txtName.Focus();
                MessageBox.Show("Chưa nhập hình thức thôi việc", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            Form2Info();
            if (_TypeOfLeaveWorkInfo.ID > 0)//update
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (wcfTypeOfLeaveWork.TypeOfLeaveWork_Update(_TypeOfLeaveWorkInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _TypeOfLeaveWorkInfo.ID = wcfTypeOfLeaveWork.TypeOfLeaveWork_Insert(_TypeOfLeaveWorkInfo);
                if (_TypeOfLeaveWorkInfo.ID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_TypeOfLeaveWorkInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _TypeOfLeaveWorkInfo = new TypeOfLeaveWorkInfo();
            _TypeOfLeaveWorkInfo.ID = 0;
            txtCode.Text = "";
            txtCode.Focus();
            txtName.Text = "";           
        }

        private void frmTypeOfLeaveWorkInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show("Dữ liệu đã thay đổi.\nBạn có muốn lưu trước khi thoát không?.", UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            wcfTypeOfLeaveWork.Close();
            this.DialogResult = dlgResult;
        }

        private void frmTypeOfLeaveWorkInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
            }
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }
    }
}
